# coding: utf-8
import uuid
from marshmallow import Schema, fields
from model.model_base import Base
from sqlalchemy import Column, String, DateTime, TIMESTAMP
from sqlalchemy.ext.hybrid import hybrid_property
import gps_transform


class WorkRecord(Base):
    # 现场工作记录表
    __tablename__ = 'WorkRecord'
    bureauid = Column(String(2), nullable=False, comment='供电局编号')
    stationid = Column(String(2), nullable=False, comment='供电所编号')
    openid = Column(String(128), nullable=False, comment='操作员微信openid')
    opentime = Column(TIMESTAMP(timezone=True), nullable=True, comment='操作员扫码时间')
    flag = Column(String(1), nullable=True, comment='专公变标志（1专变2公变3低压）')
    meter_barcode = Column(String(50), nullable=False, comment='电能表条码号')
    photo_fingerprint = Column(String(100), nullable=True, comment='图片指纹')  # 用于存储medieid
    longitude_latitude = Column(String(100), nullable=True, comment='经纬度')
    problemid = Column(String(2), nullable=True, comment='问题编号')
    resolventid = Column(String(2), nullable=True, comment='解决方法编号')
    equ_venderid = Column(String(4), nullable=True, comment='设备厂家编号')
    remark = Column(String(200), nullable=True, comment='备注')
    initial_result_flag = Column(String(1), nullable=True, default='0', comment='初始处理结果（0未抄回1已抄回）')
    final_result_flag = Column(String(1), nullable=True, default='0', comment='最终处理结果（0未解决1已解决2供电所已处理3运维组协助4运维组反馈未解决）')
    work_department = Column(String(1), nullable=True, default='2', comment='处理部门（1局方2我方）')
    confirmtime = Column(DateTime, nullable=True, comment='项目经理确认时间')
    serial_number = Column(String(32), primary_key=True, default=uuid.uuid4().hex, comment='序列号')
    confirm_openid = Column(String(128), nullable=False, comment='确认操作员微信openid')
    bureauname = Column(String(50), nullable=False, comment='县局名称')
    stationname = Column(String(50), nullable=False, comment='供电所名')
    proname = Column(String(50), nullable=False, comment='问题名称')
    resolname = Column(String(50), nullable=False, comment='解决方法名称')
    equ_vendername = Column(String(50), nullable=False, comment='设备厂家名称')
    openname = Column(String(50), nullable=False, comment='操作员名')
    confirm_openname = Column(String(50), nullable=False, comment='确认项目经理名')
    photo_flag = Column(String(1), nullable=True, default='0', comment='是否拍照（0否1是）')
    vender_flag = Column(String(1), nullable=True, default='0', comment='是否更换设备（0否1是)')
    flag_result = Column(String(1), nullable=True, comment='督办核实标志（0未解决1已解决2供电所已处理3运维组协助4运维组反馈未解决）')

    @hybrid_property
    def mars_location(self):
        ori_lon, ori_lat = tuple([float(value) for value in self.longitude_latitude.split(',')])
        lat, lon = gps_transform.transform(ori_lat, ori_lon)
        return f"{lon},{lat}"


class WorkRecordSchema(Schema):
    bureauid = fields.String()
    stationid = fields.String()
    openid = fields.String()
    opentime = fields.DateTime(format="%Y-%m-%d %H:%M:%S.%f")
    flag = fields.String()
    meter_barcode = fields.String()
    photo_fingerprint = fields.String()
    longitude_latitude = fields.String()
    problemid = fields.String()
    resolventid = fields.String()
    equ_venderid = fields.String()
    remark = fields.String()
    initial_result_flag = fields.String()
    final_result_flag = fields.String()
    work_department = fields.String()
    confirmtime = fields.DateTime(format="%Y-%m-%d %H:%M:%S.%f")
    serial_number = fields.String()
    confirm_openid = fields.String()
    bureauname = fields.String()
    stationname = fields.String()
    proname = fields.String()
    resolname = fields.String()
    equ_vendername = fields.String()
    openname = fields.String()
    confirm_openname = fields.String()
    photo_flag = fields.String()
    vender_flag = fields.String()
    flag_result = fields.String()
    mars_location = fields.String()


workRecordSchema = WorkRecordSchema()
